function searchPlayerForCondition() {
	var data = {
		"name": $("#playerName").val(),
		"role": $("#roleSelect").val(),
	}
	$("#playersForCondition").load("/searchPlayerForCondition", data);
}

function searchPlayerForMultipleCondition() {
	var data = {
		"name": $("#playerName").val(),
		"role": $("#roleSelect").val(),
	}
	$("#playersForCondition").load("/searchPlayerForMultipleCondition", data);
}

function searchTopmostPlayer() {
	$("#playersForCondition").load("/searchTopmostPlayerForCondition");
}

// 多项数据对比选择热门球员
function searchTopmostPlayerForMultiple() {
	$("#playersForCondition").load("searchTopmostPlayerForMultipleCondition");
}

// 选择所有热门球员
function chooseAllTopmostPlayer() {
	$.ajax({
		url: "listAllTopmostPlayer",
		type: "GET",
		success: function(response) {
			var playerList = response.data;
			for (var i = 0; i < playerList.length; i++) {
				var player = playerList[i]
				if (player.playerCName != null) {
					$("#choosedPlayer").append('<div class="alert alert-secondary alert-dismissible" style="width:auto; display:inline-block; margin-right: 10px; font-size: 14px;">' +
						'<button type="button" class="close" data-dismiss="alert">&times;</button>' + 
						player.playerEName + '/' + player.playerCName +
						'<input type="hidden" value="'+ playerId + '"/> ' +
						'</div>');
					// $("#choosedPlayer").append('<button type="button" class="btn btn-default" value = "' + player.playerId +
					// 	'" disabled="disabled">' + player.playerEName + '/' + player.playerCName + '</button>');
				} else {
					$("#choosedPlayer").append('<div class="alert alert-secondary alert-dismissible" style="width:auto; display:inline-block; margin-right: 10px; font-size: 14px;">' +
						'<button type="button" class="close" data-dismiss="alert">&times;</button>' + 
						player.playerEName +
						'<input type="hidden" value="'+ playerId + '"/> ' +
						'</div>');
				}
			}
		}
	});
}

function clearChoosedPlayer() {
	$('#choosedPlayer').html("")
}

// 单项数据对比选择球员
function choosePlayer(playerId, name) {
	// if ($("#choosedPlayer button").length < 2) {
		var playerIdList = [];
		var flag = false;
		for (var i = 0; i < $("#choosedPlayer div").length; i++) {
			var existingPlayerId = $("#choosedPlayer div:eq(" + i + ") input").val();
			if(playerId == existingPlayerId){
				alert("已有该球员");
				flag = true;
				break;
			}
		}
		if(!flag){
			$("#choosedPlayer").append('<div class="alert alert-secondary alert-dismissible" style="width:auto; display:inline-block; margin-right: 10px; font-size: 14px;">' +
				'<button type="button" class="close" data-dismiss="alert">&times;</button>' + 
				name +
				'<input type="hidden" value="'+ playerId + '"/> ' +
				'</div>');
		}
		// $("#choosedPlayer").append('<button type="button" class="btn btn-default" value = "' + playerId +
		// 	'" disabled="disabled">' + name + '</button>');
	// } else {
	// 	alert("只能选择两个球员");
	// }
}

// 多项数据对比选择球员
function choosePlayerForMultiple(playerId, name) {
	
	var playerIdList = [];
	var flag = false;
	for (var i = 0; i < $("#choosedPlayer div").length; i++) {
		var existingPlayerId = $("#choosedPlayer div:eq(" + i + ") input").val();
		if(playerId == existingPlayerId){
			alert("已有该球员");
			flag = true;
			break;
		}
	}
	if(!flag){
		$("#choosedPlayer").append('<div class="alert alert-secondary alert-dismissible" style="width:auto; display:inline-block; margin-right: 10px; font-size: 14px;">' +
			'<button type="button" class="close" data-dismiss="alert">&times;</button>' + 
			name +
			'<input type="hidden" value="'+ playerId + '"/> ' +
			'</div>');
	}
}

function chooseDataTypeForMultiple() {
	var type = "";
	var dataType = "";
	var count = 0;
	var dataTypeList = [];
	var dataTypeNameList = []
	var avgDataType = $("#avgSelect").val();
	var sumDataType = $("#sumSelect").val();
	var basicAdvDataType = $("#basicAdvSelect").val();
	var shootingAdvDataType = $("#shootingAdvSelect").val();

	var dataTypeCName = "";
	if (avgDataType != "") {
		type = "avg";
		dataType = avgDataType;
		dataTypeCName = $("#avgSelect option:selected").text();
		count++;
		dataTypeNameList.push(dataTypeCName);
		dataTypeList.push(dataType);
	}
	if (sumDataType != "") {
		type = "sum";
		dataType = sumDataType;
		dataTypeCName = $("#sumSelect option:selected").text();
		count++;
		dataTypeNameList.push(dataTypeCName);
		dataTypeList.push(dataType);
	}
	if (basicAdvDataType != "") {
		type = "basicAdv";
		dataType = basicAdvDataType;
		dataTypeCName = $("#basicAdvSelect option:selected").text();
		count++;
		dataTypeNameList.push(dataTypeCName);
		dataTypeList.push(dataType);
	}
	if (shootingAdvDataType != "") {
		type = "shootingAdv";
		dataType = shootingAdvDataType;
		dataTypeCName = $("#shootingAdvSelect option:selected").text();
		count++;
		dataTypeNameList.push(dataTypeCName);
		dataTypeList.push(dataType);
	}
	if (count > 2) {
		alert("只能选择两个数据维度");
		return;
	}
	for (var i = 0; i < dataTypeList.length; i++) {
		$("#choosedDataType").append('<button type="button" class="btn btn-default" value = "' + dataTypeList[i] +
			'" disabled="disabled">' + dataTypeNameList[i] + '</button>')
	}
}

function readyCompare() {
	// if ($("#choosedPlayer button").length < 2) {
	// 	alert("请选择两名球员");
	// 	return;
	// }
	if ($("#seasonTypeSelect").val() == "") {
		alert("请选择常规赛或季后赛");
		return;
	}
	var type = "";
	var dataType;
	var count = 0;
	var avgDataType = $("#avgSelect").val();
	var sumDataType = $("#sumSelect").val();
	var basicAdvDataType = $("#basicAdvSelect").val();
	var shootingAdvDataType = $("#shootingAdvSelect").val();

	var dataTypeCName = "";
	if (avgDataType != "") {
		type = "avg";
		dataType = avgDataType;
		dataTypeCName = $("#avgSelect option:selected").text();
		count++;
	}
	if (sumDataType != "") {
		type = "sum";
		dataType = sumDataType;
		dataTypeCName = $("#sumSelect option:selected").text();
		count++;
	}
	if (basicAdvDataType != "") {
		type = "basicAdv";
		dataType = basicAdvDataType;
		dataTypeCName = $("#basicAdvSelect option:selected").text();
		count++;
	}
	if (shootingAdvDataType != "") {
		type = "shootingAdv";
		dataType = shootingAdvDataType;
		dataTypeCName = $("#shootingAdvSelect option:selected").text();
		count++;
	}
	if (count > 1) {
		alert("只能选择一个数据维度");
		return;
	}

	var playerIdList = [];
	for (var i = 0; i < $("#choosedPlayer div").length; i++) {
		playerIdList.push($("#choosedPlayer div:eq(" + i + ") input").val());
	}

	var data = {
		"playerIdList": playerIdList,
		"seasonType": $("#seasonTypeSelect").val(),
		"type": type,
		"dataType": dataType,
		"dataTypeCName": dataTypeCName
	}

	$.ajax({
		url: "singlePlayerCompare",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(response) {
			var myChart = echarts.init($("#playerCompareContent").get(0));

			var seasonList = response.data.seasonList;
			var playerList = response.data.playerList;

			var xAxis = [];
				
			for (var i = 0; i < seasonList.length; i++) {
				var season = seasonList[i];
				xAxis.push(season.substr(2) + "-" + String((Number(season) + 1)).substr(2));
			}
			var echartsSeries = [];
			var legendName = [];
			for(var i = 0; i<playerList.length; i++){
				var series = {
					name : playerList[i].playerCName,
					type : 'line'
				}
				legendName.push(playerList[i].playerCName);
				var data = [];
				for(var j=0; j<seasonList.length; j++){
					var season = seasonList[j];
					if (playerList[i].dataMap[season] == null) {
						data.push(0)
					} else {
						data.push(playerList[i].dataMap[season])
					}
				}
				series['data'] = data;
				echartsSeries.push(series);
			}

			var option = {
				title: {
					text: '球员' + dataTypeCName + '对比图'
				},
				legend: {
					show: true,
					data: legendName
				},
				dataZoom: [
					{ // 这个dataZoom组件，默认控制x轴。
						type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
						start: 0, // 左边在 0% 的位置。
						end: 100 ,// 右边在 60% 的位置。
						xAxisIndex: 0,
						filterMode: 'filter'
					},
					{
						type: 'slider',
						start: 0, // 左边在 0% 的位置。
						end: 100, // 右边在 60% 的位置。
						yAxisIndex: 0,
						filterMode: 'empty'
					}
				],
				tooltip: {
					trigger: "axis",
				},
				xAxis: {
					data: xAxis
				},
				yAxis: {
					type: 'value'
				},
				series: echartsSeries
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option, true);
		}
	});
}

function readyCompareForMultiple() {
	if ($("#choosedPlayer button").length < 1) {
		alert("请选择球员");
		return;
	}

	if ($("#seasonTypeSelect").val() == "") {
		alert("请选择常规赛或季后赛");
		return;
	}

	if ($("#seasonSelect").val() == "") {
		alert("请选择赛季");
		return;
	}

	var selectList = $("#dataType").children("select");
	var selectList2 = $("#dataType2").children("select");

	var type = "";
	var type2 = "";
	var dataType = "";
	var dataType2 = "";
	var dataTypeCName = "";
	var dataTypeCName2 = "";

	var count = 0;

	selectList.each(function(i) {
		if ($(this).val() != "") {
			switch (i) {
				case 0:
					type = "avg";
					break;
				case 1:
					type = "sum";
					break;
				case 2:
					type = "basicAdv";
					break;
				case 3:
					type = "shootingAdv";
					break;
			}
			dataType = $(this).val();
			dataTypeCName = $(this).children("option:selected").text();
			count++;
		}
	})

	// alert(type)
	// alert(dataType)
	// alert(dataTypeCName)

	if (count < 1) {
		alert("数据维度一未选择")
	}
	if (count > 1) {
		alert("只能选择两个数据维度")
		return;
	}
	count = 0;
	selectList2.each(function(i) {
		if ($(this).val() != "") {
			switch (i) {
				case 0:
					type2 = "avg";
					break;
				case 1:
					type2 = "sum";
					break;
				case 2:
					type2 = "basicAdv";
					break;
				case 3:
					type2 = "shootingAdv";
					break;
			}
			dataType2 = $(this).val();
			dataTypeCName2 = $(this).children("option:selected").text();
			count++;
		}
	});
	// alert(type2)
	// alert(dataType2)
	// alert(dataTypeCName2)
	if (count < 1) {
		alert("数据维度二未选择")
	}
	if (count > 1) {
		alert("只能选择两个数据维度")
		return;
	}

	var playerIdList = [];
	for (var i = 0; i < $("#choosedPlayer div").length; i++) {
		playerIdList.push($("#choosedPlayer div:eq(" + i + ") input").val());
	}

	var data = {
		"playerIdList": playerIdList,
		"seasonType": $("#seasonTypeSelect").val(),
		"typeList": [type, type2],
		"dataTypeList": [dataType, dataType2],
		"season": $("#seasonSelect").val()
	}

	$.ajax({
		url: "multiplePlayerCompare",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify(data),
		success: function(response) {
			var myChart = echarts.init($("#playerMultipleCompareContent").get(0));

			var data = response.data.dataList;

			option = {
				title: {
					text: '球员多项数据对比图'
				},
				xAxis: {
					name: dataTypeCName,
					scale: true
				},
				yAxis: {
					name: dataTypeCName2,
					scale: true
				},
				label: {
					show: true,
					position: [10, 10],
					formatter: "{@[2]}",
					color: "#000000"
				},
				color: '#627683',
				dataZoom: [
					{ // 这个dataZoom组件，默认控制x轴。
						type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
						start: 0, // 左边在 0% 的位置。
						end: 100 ,// 右边在 60% 的位置。
						xAxisIndex: 0,
						filterMode: 'filter'
					},
					{
						type: 'slider',
						start: 0, // 左边在 0% 的位置。
						end: 100, // 右边在 60% 的位置。
						yAxisIndex: 0,
						filterMode: 'empty'
					}
				],
				series: [{
					type: 'scatter',
					data: data
				}]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option, true);
		}
	});

}
